if('WebSocket' in window){//判断当前浏览器是否支持WebSocket
    websocket = new WebSocket("ws://123.207.37.11:8080/websocket");
    // websocket = new WebSocket("ws://localhost:8080/websocket");
}
else{
    alert('Not support websocket')
}
websocket.onopen=function () {//建立连接时
    var oPerson=new Ball(userId,userName,randPosition.x,randPosition.y,50,50);
    oPerson.oDiv.css({'background':'cornflowerblue'});
    person.push(oPerson);
    var message=JSON.stringify({'state':'join','userId':person[0].userId,'userName':person[0].userName,'x':person[0].x,'y':person[0].y,
    'width':person[0].width,'height':person[0].height});
    websocket.send(message);
}
websocket.onmessage=function (event) {//收到消息时
    var message=JSON.parse(event.data);
    if(message.state=='bullet'&&message.userId!=person[0].userId){
        var oBullet=new Bullet(message.x1,message.y1,message.x2,message.y2,message.width,message.height);
        var i=1;
        for (;i<person.length;i++){
            if(person[i].userId==message.userId){
                break;
            }
        }
        person[i].bullet.push(oBullet);
        return;
    }
    if(message.state=='join'&&message.userId!=person[0].userId){
        var flag=false;
        for (var i=1;i<person.length;i++){
            if(person[i].userId==message.userId){
                flag=true;
                break;
            }
        }
        if(!flag){//说明有新的person
            person.push(new Ball(message.userId,message.userName,message.x,message.y,message.width,message.height));
            // 有人加入，发送自己的信息给其他人，自己还活着的情况下
            if(person[0].death==false){
                var message=JSON.stringify({'state':'join','userId':person[0].userId,'userName':person[0].userName,
                    'x':person[0].x,'y':person[0].y,'width':person[0].width,'height':person[0].height});
                websocket.send(message);
            }
        }
        return;
    }
    if(message.state=='move'&&message.userId!=person[0].userId){
        var i=1;
        for (;i<person.length;i++){
            if(person[i].userId==message.userId){
                break;
            }
        }
        person[i].x=message.x;
        person[i].y=message.y;
        return;
    }
    if(message.state=='exit'){
        var i=1;
        for (;i<person.length;i++){
            if(person[i].userId==message.userId){
                break;
            }
        }
        person[i].exit();
        person.splice(i,1);
    }
    if(message.state=='death'){
        var i=0;
        for (;i<person.length;i++){
            if(person[i].userId==message.userId){
                break;
            }
        }
        person[i].death=true;
        person[i].oDiv.css({
            'background':'gray'
        })
    }
    if(message.state=='setSize'){
        var i=0;
        for (;i<person.length;i++){
            if(person[i].userId==message.userId){
                break;
            }
        }
        person[i].width=message.width;
        person[i].height=message.height;
        person[i].oDiv.css({
            'width':person[i].width+'px',
            'height':person[i].height+'px'
        })
    }
}